 //全局预加载
var dataList = [
	path+"/images/guang_bg.jpg",
	path+"/images/zhou_bg.jpg",
	path+"/images/guo_bg.jpg",
	path+"/images/nian_bg.jpg",
	path+"/images/hua1_bg.jpg",
	path+"/images/cheng_bg.jpg",
	path+"/images/kan_bg.jpg",
	path+"/images/hua2_bg.jpg",
//	path+"/images/guang_card.jpg",
//	path+"/images/zhou_card.jpg",
//	path+"/images/guo_card.jpg",
//	path+"/images/nian_card.jpg",
//	path+"/images/hua1_card.jpg",
//	path+"/images/cheng_card.jpg",
//	path+"/images/kan_card.jpg",
//	path+"/images/hua2_card.jpg",
	path+"/images/guang_ori.png",
	path+"/images/zhou_ori.png",
	path+"/images/guo_ori.png",
	path+"/images/nian_ori.png",
	path+"/images/hua1_ori.png",
	path+"/images/cheng_ori.png",
	path+"/images/kan_ori.png",
	path+"/images/hua2_ori.png",
	path+"/images/hongbao_close.png",
	path+"/images/hongbao_open.png",
	path+"/images/hongbao_front.png",
	path+"/images/yanhua.png",
	path+"/images/waiting_hua.png",
	path+"/images/index_btn_text.png"
];

 var queue = new createjs.LoadQueue();
 queue.on("complete", function(){
 	$("img").each(function(){
 		$(this).attr("src",$(this).data("src"))
 	})
 	$(".startLoading").css("display","none");
	$(".index_web").css("display","block");
 }, this);
 queue.on("fileload", function(e){
 	loaded++;
   	$(".startLoading span").text(Math.ceil(loaded/total*100)+"%")
 },this);
 queue.loadManifest(dataList);

var total = dataList.length;
var loaded = 0; //已经被加载的数量
var percent = 0; //加载百分比
//digui();
//beginLoad();
//var imgs;
//function beginLoad(){
//	imgs = document.querySelectorAll("img");
//	total = imgs.length;
//	for(var i=0;i<total;i++){
//		load(i);
//	}
//}

//function loadCallback(i){
//	imgs[i].setAttribute("src",imgs[i].dataset.src);
//	//每次加载完一张图片的函数
//	$(".startLoading span").text(Math.ceil(loaded/total*100)+"%")
//	if(loaded >= total) {//加载完成
//		$(".startLoading").css("display","none");
//		$(".index_web").css("display","block");
//	}
//}
//function load(i){
//	loaded++;
//	console.log(loaded)
//	var tmp = new Image();
//	//加载完成后，触发该回调操作
//	tmp.addEventListener("load", function() {
//		loadCallback(i)
//	})
//	//加载完成后，触发该回调操作
//	tmp.addEventListener("error", function() {
//		console.log("图片加载错误");
//		loadCallback(i)
//	})
//	//赋地址
//	tmp.src = imgs[i].dataset.src;
//}
//function beginLoad(){
//	for(var i in dataList){
//		var tmp = new Image();
//		//加载完成后，触发该回调操作
//		tmp.addEventListener("load", function() {
//				loadFun();
//			})
//			//加载完成后，触发该回调操作
//		tmp.addEventListener("error", function() {
//			console.log("图片加载错误");
//			loadFun();
//		})
//		//赋地址
//		tmp.src = dataList[i];
//	}
//}

//加载一张图或出错时候，触发该回调操作
//function loadFun(){
//	loaded++;
//	console.log(loaded)
//	//每次加载完一张图片的函数
//	$(".startLoading span").text(Math.ceil(loaded/total*100)+"%")
//	if(loaded == total) {//加载完成
//		//alert("jiazaiwancheng");
//		$(".startLoading").css("display","none");
//		$(".index_web").css("display","block");
//		//$(".web_wrap").css("display","block");
//	//预加载层关闭
//	}
//}


function digui() {
	var tmp = new Image();
	//加载完成后，触发该回调操作
	tmp.addEventListener("load", function() {
		loadFun();
	})
		//加载完成后，触发该回调操作
	tmp.addEventListener("error", function() {
		console.log("图片加载错误");
		loadFun();
	})
	//赋地址
	tmp.src = dataList[i];
	
}

//加载一张图或出错时候，触发该回调操作
//function loadFun() {
//	loaded++;
//	i++;
//	console.log(loaded)
//	//每次加载完一张图片的函数
//	$(".startLoading span").text(Math.ceil(loaded/total*100)+"%")
//	if(loaded == total) {//加载完成
//		//alert("jiazaiwancheng");
//		$(".startLoading").css("display","none");
//		$(".index_web").css("display","block");
//		//$(".web_wrap").css("display","block");
//	//预加载层关闭
//	} else {
//		digui();
//	//递归
//	}
//}




//背景动画
function scroll_bg(){
	TweenMax.set($(".scroll_bg img"),{
		y:"-100%"
	})
	TweenMax.to($(".scroll_bg img"),18,{
		y:"0%",
		ease:Power0.easeNone,
		onComplete:function(){
			$(".scroll_bg img ").last().prependTo(".scroll_bg");
			scroll_bg();
		}
	})

}
scroll_bg()
//红包动画
var i=0;
var t1;
function scale(){
	var curTar = $(".content .hb_list").eq(i);
	t1 = new TimelineMax();
	t1.to(
		curTar,1,{
			y:-30,
			ease:  Power3.easeOut
		}
	).to(
		curTar,1,{
			y:0,
			ease: Elastic.easeOut.config(1, 0.3),
			onComplete:function(){
				curTar.attr("style","");
//				i = Math.floor(Math.random()*7);
				i++;
				if(i>7){
					i=0;
				}
				scale();
			}
		}
	)
}
scale();

//点击放大
var bodyHeight = $("body").height();
var bodyWidth = $("body").width();
var beginTimeLine;//开场动画时间轴
var canTouch=true;//开场红包点击开关
/*首页的红包点击事件**/
$(".content .hb_list").on("touchstart",function(){
	
	if(canTouch){
		var _s = $(this);
		var curPos = $(this).offset();
		var halfX = _s.width()*.5;
		var halfY = _s.height()*.5;
		var index =_s.find("img").eq(1).data('id');
		
		var 
			hongbao_front = $(".hongbao_front"),
			guang_card = $(".guang_card"),
			writing_web = $(".writing_web")
			writing_web_wrap = $(".writing_web_wrap")	
		beginTimeLine = new  TimelineMax({paused:true});	
		beginTimeLine
		.call(function(){
			t1.paused()?t1.resume():t1.pause();
			canTouch=!canTouch;
		})
		.to(_s.siblings(),.2,{
			autoAlpha:0,
		})
		.to($(".footer"),.2,{
			autoAlpha:0,
		},-.2)
		.to(_s,1,{
			x:bodyWidth*.5-curPos.left-halfX,
			y:bodyHeight*.5-curPos.top-halfY+100,
			scale:3
		})
		.call(function(){
			var src = _s.find(".close").attr("src");
			src = src==hongbao_open_src?hongbao_close_src:hongbao_open_src
			_s.find(".close").attr("src",src)
		})
		.to(hongbao_front,.4,{
			autoAlpha:1
		})
		.to(guang_card,.3,{
			y:"-40%",
			autoAlpha:1,
		})
		.to(guang_card,1,{
			delay:.8,
			y:-1600,
			onComplete:function(){
				//设置对应写字界面的文字以及背景
				curIndex = index;
				$(".writing_web .main_text img").attr("src",textArr[index][0]);
				$(".writing_web").css("background-image","url("+textArr[index][1]+")");
				showMask("pointer");
				initCanvas(curIndex);
			}
		})
		.to(writing_web_wrap,.1,{
			display:"block"
		})
		.fromTo(writing_web_wrap,.8,{
			delay:1,
			y:-1600
		},{
			y:0,
			ease:Power0.easeNone
		})
		
		beginTimeLine.play();
	}
})





/****
 * 显示对应遮罩
 */
function showMask(mask){
	var maskEl = $(".mask");
	maskEl.off("touchstart");
	switch(mask){
		case "pointer":
			maskEl.on("touchstart",function(){
				hideMask();
			});
			maskEl.find(".pointer").css("display","block");
			maskEl.find(".share").css("display","none");
			maskEl.find(".waiting").css("display","none");
			break;
		case "pointer2"://这个遮罩不带有点击消失的事件
			maskEl.find(".pointer").css("display","block");
			maskEl.find(".share").css("display","none");
			maskEl.find(".waiting").css("display","none");
			break;
		case "share":
			maskEl.find(".pointer").css("display","none");
			maskEl.find(".share").css("display","block");
			maskEl.find(".waiting").css("display","none");
			
			//绑定跳转到结尾动画
			maskEl.on("touchstart",function(){
				gotoEnd();
			});
			
			break;
		case "waiting":
			maskEl.find(".pointer").css("display","none");
			maskEl.find(".share").css("display","none");
			maskEl.find(".waiting").css("display","block");
			break;
	}
	TweenMax.to(maskEl,.5,{autoAlpha:1})
}
/****
 *	隐藏遮罩
 */
function hideMask(mask){
	TweenMax.to($(".mask"),.5,{autoAlpha:0})
}

/*******结尾动画*******/
function gotoEnd(){
	hideMask();
	if(userScore>0){
		var t = new  TimelineMax();	
		t
		.call(function(){
			$("#show_wenzi").attr("src",userImg);//更改成用户所写的文字
			$("#targetImg_bg").attr("style",'background: url("'+textArr[curIndex][1]+'") 0% 0% / 100% 100% no-repeat;');
			$(".web_wrap").hide();
			$(".show_web_wrap").show();
			$(".show_web_wrap").css("opacity",0);
		})
		.to($(".show_web_wrap"),1,{autoAlpha:1})
		.call(function(){
			$(".show_web li img").show();
			//获取目标位置图片的左上角位置
			var tarImg = $("li").eq(curIndex).find("img");
			var tarImgOff = tarImg.offset();
			tarImg.hide();
			//加回来滚动背景
			$(".show_web .scroll_bg").css("display","block");
			//移动距离show.js
			var X = 320-tarImgOff.left-tarImg.width()*.5;
			var Y = 420-tarImgOff.top-tarImg.height()*.5;
			
			TweenMax.to($("#show_wenzi"),1.5,{
				scale:0.15,
				x:-X,
				y:-Y
			})
			var tar = $(".show_web .content .targetImg_bg");
			TweenMax.to(tar,1,{
				opacity:0,
				onComplete:function(){
					tar.hide();
				}
			})
			//给中间文字以及底部按钮加动画
			$(".wel_second img").eq(0).addClass("animated pulse infinite");
			$(".wel_second img").eq(1).addClass("animated pulse infinite");
			$(".wel_second img").eq(1).css("animation-delay","1000ms");
			$(".show_web .btn img").addClass("animated pulse infinite");
			
		});
	}
	
}

var bg_mask = $("#bg_mask");
var wipeWrap = $("#wipe");
var bg_mask_opacity;
var t;
var wipe;
var userScore=0;
var userImg;//用户所写的图片
document.addEventListener("touchmove",function(e){
	e.preventDefault();
},false)
function startT(){
	t =setInterval(function(){
		bg_mask_opacity -=.015;
		bg_mask.css("opacity",bg_mask_opacity);
	},100)
};
function endT(){
	clearInterval(t);
}
//初始化canvas
function initCanvas(index,callback){
	callback = typeof(callback)!='undefined'?callback:function(){};
	userScore = 0;//初始化分数
	bg_mask_opacity = .4;//初始化背景亮度
	bg_mask.css("opacity",bg_mask_opacity);
	if(t){
		clearInterval(t);
	}
	t=0;
	wipe=null;
	wipe = new Wipe({
		el: '#wipe',
		fg: textArr[index][2],
		size: 50,
		debug: false,
		imgWidth:580,
		imgHeight:580,
        lineMax : 60,		//笔画最大粗
	    lineMin : 20, 		//笔画最小粗
		color: "rgba(34,23,20,1)",//笔触颜色，褐色
		autoWipe: false,
		data: null,
//		onswiping: function (percent) {
////							console.log(percent);
//		},
		callback:callback
	})
	wipe.lineMax = 50;  
	wipe.lineMin = 30;
	wipe.linePressure = 2;  
	wipe.smoothness = 100; 
	
	//把按钮重新显示出来
	$(".beforeWri").show();
	$(".afterWri").hide();
	
	
	//把用户评分隐藏
	$(".userScore").hide();
	
	//把文字背景重新显示出来
	$("#zi_bg").show();
	
	
}

